package com.heima.article.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.heima.article.dto.*;
import com.heima.article.entity.ApArticle;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.article.service.IApArticleService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 文章信息表，存储已发布的文章 前端控制器
 * </p>
 *
 * @author mcm
 * @since 2021-11-11
 */
@RestController
@RequestMapping("/api/v1/article")
@Api(tags = "文章信息表，存储已发布的文章接口")
@CrossOrigin
public class ApArticleController {

    @Autowired
    private IApArticleService apArticleService;

    /**
     * 保存文章
     *
     * @param dto
     * @return
     */
    @PostMapping
    public ResponseResult<Long> saveArticle(@RequestBody ArticleDto dto) {
        return apArticleService.saveArticle(dto);
    }


    /**
     * 加载文章列表
     *
     * @param dto
     * @return
     */
    @PostMapping("/load")
    public ResponseResult load(@RequestBody ArticleHomeDto dto) {
        return apArticleService.load(dto, 0);
    }

    /**
     * 加载更多文章
     *
     * @param dto
     * @return
     */
    @PostMapping("/loadmore")
    public ResponseResult loadMore(@RequestBody ArticleHomeDto dto) {
        return apArticleService.load(dto, 1);
    }

    /**
     * 加载最新文章
     *
     * @param dto
     * @return
     */
    @PostMapping("/loadnew")
    public ResponseResult loadNew(@RequestBody ArticleHomeDto dto) {
        return apArticleService.load(dto, 0);
    }

    /**
     * 加载文章详情
     *
     * @param dto
     * @return
     */
    @PostMapping("/load_article_info")
    public ResponseResult loadArticleInfo(@RequestBody ArticleInfoDto dto) {
        return apArticleService.loadArticleInfo(dto);
    }


    /**
     * 文章收藏
     *
     * @param dto
     * @return
     */
    @PostMapping("/collection")
    public ResponseResult collect(@RequestBody CollectionBehaviorDto dto) {
        return apArticleService.collect(dto);
    }

    /**
     * 加载文章行为
     *
     * @param dto
     * @return
     */
    @PostMapping("/load_article_behavior")
    public ResponseResult loadArticleBehavior(@RequestBody ArticleInfoDto dto) {
        return apArticleService.loadArticleBehavior(dto);
    }

    @PostMapping("/load_article_comments")
    public ResponseResult loadComments(@RequestBody CommentDto dto){
        return apArticleService.loadComments(dto);
    }

    @PostMapping("/update_comment_status")
    public ResponseResult updateComment(@RequestBody CommentStatus dto){
        return apArticleService.updateComment(dto);
    }

    /**
     * 图文统计
     * @param dto
     * @return
     */
    @GetMapping("/newsDimension")
    public ResponseResult newsDimension(NewsDimensionDto dto){
        return apArticleService.newsDimension(dto);
    }
    @GetMapping("/getNewsPageStatistics")
    public ResponseResult getNewsPageStatistics(NewsDimensionDto dto){
        return apArticleService.getNewsPageStatistics(dto);
    }

    @PostMapping("/articleInfo")
    public ApArticle articleInfo(@RequestBody Long id){
        return apArticleService.getById(id);
    }

    /**
     * 更新评论数
     */
    @PostMapping("/update")
    public ResponseResult<ApArticle> update(@RequestParam Long id){

        ApArticle apArticle = apArticleService.getById(id);

        Integer comment = apArticle.getComment();

        comment = ++comment;

        apArticle.setComment(comment);

        // 更新回表
        apArticleService.updateById(apArticle);

        return ResponseResult.okResult();

    }
    @PostMapping("/findArticle")
    public ApArticle findArticle(@RequestBody Long id){
        return apArticleService.getById(id);
    }

}
